//
//  DetailViewController.m
//  ThiNhom_iOS
//
//  Created by admin on 5/31/15.
//  Copyright (c) 2015 Huynh Tri Dung. All rights reserved.
//

#import "DetailViewController.h"

@interface DetailViewController ()
@property (strong, nonatomic) IBOutlet UIImageView *bgImgView;
@property (strong, nonatomic) IBOutlet UIImageView *foodImgView;
@property (strong, nonatomic) IBOutlet UITextView *foodDetailTf;
@property (strong, nonatomic) IBOutlet UIButton *prevBtn;
@property (strong, nonatomic) IBOutlet UIButton *nextBtn;

@property (strong, nonatomic) NSDictionary* recipe;
@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"%ld", (long)self.currrentFoodIndex);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [self setView];
}

- (void)getData{
    NSDictionary* currentRecipe = [self.allSearchedFood objectAtIndex:self.currrentFoodIndex];
    NSLog(@"%@",self.allSearchedFood);
    NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://food2fork.com/api/get?key=2d64c4bc219c56f7509860a5afa36fe1&rId=%@",[currentRecipe objectForKey:@"recipe_id"]]]];
    NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    self.recipe = [json objectForKey:@"recipe"];
}

- (void)setView{
    [self getData];
    NSMutableArray* description = [self.recipe objectForKey:@"ingredients"];
    NSString* descriptionStr = @"";
    for (NSString* line in description) {
        descriptionStr = [descriptionStr stringByAppendingString:[NSString stringWithFormat:@"%@\n",line]];
    }
    NSLog(@"%@",[description objectAtIndex:0]);
    self.foodDetailTf.text = descriptionStr;
    
    dispatch_async(dispatch_queue_create("MyQueue", NULL), ^{
        NSURL* recipeImgUrl = [NSURL URLWithString:[self.recipe objectForKey:@"image_url"]];
        NSData* data = [NSData dataWithContentsOfURL:recipeImgUrl];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.foodImgView.image = [UIImage imageWithData:data];
            self.bgImgView.image = [UIImage imageWithData:data];
        });
    });
}

-(void)makeBlur{
    
}
- (IBAction)onPrevBtnTouchDown:(id)sender {
    if (self.currrentFoodIndex>0) {
        self.currrentFoodIndex--;
        [self setView];
    }
}
- (IBAction)onNextBtnTouchDown:(id)sender {
    if (self.currrentFoodIndex<self.allSearchedFood.count-1) {
        self.currrentFoodIndex++;
        [self setView];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
